package com.zbkj.admin.service.impl;

import com.github.pagehelper.PageInfo;
import com.zbkj.admin.service.IntegralService;
import com.zbkj.common.constants.SysConfigConstants;
import com.zbkj.common.request.IntegralPageSearchRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.IntegralConfigResponse;
import com.zbkj.common.response.IntegralRecordPageResponse;
import com.zbkj.common.vo.MyRecord;
import com.zbkj.service.service.SystemConfigService;
import com.zbkj.service.service.UserIntegralRecordService;
import com.zbkj.service.util.BrandUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 积分服务实现类
 +----------------------------------------------------------------------
 */
@Service
public class IntegralServiceImpl implements IntegralService {

    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private UserIntegralRecordService userIntegralRecordService;

    /**
     * 获取积分配置
     *
     * @return IntegralConfigResponse
     */
    @Override
    public IntegralConfigResponse getConfig() {
        if (BrandUtil.isBrand()){
            List<String> keyLists = new ArrayList<>();
            keyLists.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH_BRAND);
            keyLists.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY_BRAND);
            keyLists.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY_BRAND);
            keyLists.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO_BRAND);
            keyLists.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE_BRAND);
            keyLists.add(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME_BRAND);
            keyLists.add(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE_BRAND);
            MyRecord records = systemConfigService.getValuesByKeyList(keyLists);
           /* IntegralConfigResponse configResponses = new IntegralConfigResponse();
            configResponses.setIntegralDeductionSwitch(Boolean.valueOf(records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH_BRAND)));
            configResponses.setIntegralDeductionStartMoney(Integer.valueOf(records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY_BRAND)));
            configResponses.setIntegralDeductionMoney(new BigDecimal(records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY_BRAND)));
            configResponses.setIntegralDeductionRatio(Integer.valueOf(records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO_BRAND)));
            configResponses.setOrderGiveIntegral(Integer.valueOf(records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE_BRAND)));
            configResponses.setFreezeIntegralDay(Integer.valueOf(records.getStr(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME_BRAND)));
            configResponses.setIntegralFreezeNode(records.getStr(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE_BRAND));*/
            IntegralConfigResponse configResponses = new IntegralConfigResponse();
            configResponses.setIntegralDeductionSwitch(Boolean.valueOf(records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH_BRAND)));
            String startMoneyStr = records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY_BRAND);
            configResponses.setIntegralDeductionStartMoney(StringUtils.isNotBlank(startMoneyStr) ? Integer.valueOf(startMoneyStr) : 0);
            String deductionMoneyStr = records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY_BRAND);
            configResponses.setIntegralDeductionMoney(StringUtils.isNotBlank(deductionMoneyStr) ? new BigDecimal(deductionMoneyStr) : BigDecimal.ZERO);
            String deductionRatioStr = records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO_BRAND);
            configResponses.setIntegralDeductionRatio(StringUtils.isNotBlank(deductionRatioStr) ? Integer.valueOf(deductionRatioStr) : 0);
            String orderGiveIntegralStr = records.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE_BRAND);
            configResponses.setOrderGiveIntegral(StringUtils.isNotBlank(orderGiveIntegralStr) ? Integer.valueOf(orderGiveIntegralStr) : 0);
            String freezeDayStr = records.getStr(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME_BRAND);
            configResponses.setFreezeIntegralDay(StringUtils.isNotBlank(freezeDayStr) ? Integer.valueOf(freezeDayStr) : 0);
            configResponses.setIntegralFreezeNode(records.getStr(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE_BRAND));
            return configResponses;
        }
        List<String> keyList = new ArrayList<>();
        keyList.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH);
        keyList.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY);
        keyList.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY);
        keyList.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO);
        keyList.add(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE);
        keyList.add(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME);
        keyList.add(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE);
        MyRecord record = systemConfigService.getValuesByKeyList(keyList);
        IntegralConfigResponse configResponse = new IntegralConfigResponse();
        configResponse.setIntegralDeductionSwitch(Boolean.valueOf(record.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH)));
        configResponse.setIntegralDeductionStartMoney(Integer.valueOf(record.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY)));
        configResponse.setIntegralDeductionMoney(new BigDecimal(record.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY)));
        configResponse.setIntegralDeductionRatio(Integer.valueOf(record.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO)));
        configResponse.setOrderGiveIntegral(Integer.valueOf(record.getStr(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE)));
        configResponse.setFreezeIntegralDay(Integer.valueOf(record.getStr(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME)));
        configResponse.setIntegralFreezeNode(record.getStr(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE));
        return configResponse;
    }
    /**
     * 编辑积分配置
     *
     * @param request 积分配置请求对象
     * @return Boolean
     */
    @Override
    public Boolean updateConfig(IntegralConfigResponse request) {
        return transactionTemplate.execute(e -> {
            if (BrandUtil.isBrand()){
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH_BRAND,
                        request.getIntegralDeductionSwitch().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY_BRAND,
                        request.getIntegralDeductionStartMoney().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY_BRAND,
                        request.getIntegralDeductionMoney().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO_BRAND,
                        request.getIntegralDeductionRatio().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE_BRAND,
                        request.getOrderGiveIntegral().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME_BRAND,
                        request.getFreezeIntegralDay().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE_BRAND,
                        request.getIntegralFreezeNode());
            }
            systemConfigService.updateOrSaveValueByName(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH,
                    request.getIntegralDeductionSwitch().toString());
            systemConfigService.updateOrSaveValueByName(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY,
                    request.getIntegralDeductionStartMoney().toString());
            systemConfigService.updateOrSaveValueByName(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY,
                    request.getIntegralDeductionMoney().toString());
            systemConfigService.updateOrSaveValueByName(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO,
                    request.getIntegralDeductionRatio().toString());
            systemConfigService.updateOrSaveValueByName(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE,
                    request.getOrderGiveIntegral().toString());
            systemConfigService.updateOrSaveValueByName(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME,
                    request.getFreezeIntegralDay().toString());
            systemConfigService.updateOrSaveValueByName(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE,
                    request.getIntegralFreezeNode());
            return Boolean.TRUE;
        });
    }

    @Override
    public Boolean updateConfigBrand(IntegralConfigResponse request) {
        return transactionTemplate.execute(e -> {
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_SWITCH_BRAND,
                        request.getIntegralDeductionSwitch().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_START_MONEY_BRAND,
                        request.getIntegralDeductionStartMoney().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_MONEY_BRAND,
                        request.getIntegralDeductionMoney().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_DEDUCTION_RATIO_BRAND,
                        request.getIntegralDeductionRatio().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_INTEGRAL_RATE_ORDER_GIVE_BRAND,
                        request.getOrderGiveIntegral().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_EXTRACT_TIME_BRAND,
                        request.getFreezeIntegralDay().toString());
                systemConfigService.updateOrSaveValueByNameBrand(SysConfigConstants.CONFIG_KEY_STORE_INTEGRAL_FREEZE_NODE_BRAND,
                        request.getIntegralFreezeNode());
            return Boolean.TRUE;
        });
    }

    /**
     * 积分记录分页列表
     *
     * @param request     搜索参数
     * @param pageRequest 分页参数
     */
    @Override
    public PageInfo<IntegralRecordPageResponse> findRecordPageList(IntegralPageSearchRequest request, PageParamRequest pageRequest) {
        return userIntegralRecordService.findRecordPageListByPlat(request, pageRequest);
    }
}
